/*!
 * pull to refresh v2.0
 *author:wallace
 *2015-7-22
 *
 */
(function($){

	
	var onScrolling = function(e, pullDownEl, pullUpEl, pullUpOffset) {
		if (e.y > -(pullUpOffset)) {
			pullDownEl.id = '';
			pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullDownLable;
			e.minScrollY = -pullUpOffset;
		}
		if (e.y > 0) {
			pullDownEl.classList.add("flip");
			pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullingDownLable;
			e.minScrollY = 0;
		}
		if (e.scrollerH < e.wrapperH && e.y < (e.minScrollY - pullUpOffset) || e.scrollerH > e.wrapperH && e.y < (e.maxScrollY - pullUpOffset)) {
			pullUpEl.style.display = "block";
			pullUpEl.classList.add("flip");
			pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullingUpLable;
		}
		if (e.scrollerH < e.wrapperH && e.y > (e.minScrollY - pullUpOffset) && pullUpEl.id.match('flip') || e.scrollerH > e.wrapperH && e.y > (e.maxScrollY - pullUpOffset) && pullUpEl.id.match('flip')) {
			pullDownEl.classList.remove("flip");
			pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullUpLable;
		}
	};
	var onRelease = function(pullDownEl, pullUpEl) {
		if (pullDownEl.className.match('loading')) {
			pullDownEl.classList.toggle("loading");
			pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullDownLable;
			pullDownEl.querySelector('.loader').style.display = "none"
			pullDownEl.style.lineHeight = pullDownEl.offsetHeight + "px";
		}
		if (pullUpEl.className.match('loading')) {
			pullUpEl.classList.toggle("loading");
			pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullUpLable;
			pullUpEl.querySelector('.loader').style.display = "none"
			pullUpEl.style.lineHeight = pullUpEl.offsetHeight + "px";
		}
	};
	var onPulling =  function(pullDownEl, pullDownAction, pullUpEl, pullUpAction) {
		if (pullDownEl.className.match('flip') /*&&!pullUpEl.className.match('loading')*/ ) {
			pullDownEl.classList.add("loading");
			pullDownEl.classList.remove("flip");
			pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.loadingLable;
			pullDownEl.querySelector('.loader').style.display = "block"
			pullDownEl.style.lineHeight = "20px";
			if (pullDownAction) pullDownAction();
		}
		if (pullUpEl.className.match('flip') /*&&!pullDownEl.className.match('loading')*/ ) {
			pullUpEl.classList.add("loading");
			pullUpEl.classList.remove("flip");
			pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.loadingLable;
			pullUpEl.querySelector('.loader').style.display = "block"
			pullUpEl.style.lineHeight = "20px";
			if (pullUpAction) pullUpAction();
		}
	};
	
	var scrollIt = function(parameter, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset) {
//		eval(parameter.id 
//				+ "= new iScroll(parameter.id, " +
//						"{useTransition: true," +
//							"vScrollbar: false," +
//							"topOffset: pullDownOffset," +
//							"onRefresh: function () {" +
//								"refresher.onRelease(pullDownEl,pullUpEl);" +
//							"}," +
//							"onScrollMove: function () {" +
//								"refresher.onScrolling(this,pullDownEl,pullUpEl,pullUpOffset);" +
//							"}," +
//						"onScrollEnd: function () {" +
//							"refresher.onPulling(pullDownEl,parameter.pullDownAction,pullUpEl,parameter.pullUpAction);" +
//							"}," +
//						"})"
//			);
		
		new iScroll(parameter.id, {
										useTransition: true,
										vScrollbar: false,
										topOffset: pullDownOffset,
										onRefresh: function () {
											onRelease(pullDownEl,pullUpEl);
										},
										onScrollMove: function () {
											onScrolling(this,pullDownEl,pullUpEl,pullUpOffset);
										},
										onScrollEnd: function () {
											onPulling(pullDownEl,parameter.onDown,pullUpEl,parameter.onUp);
										},
									});
		pullDownEl.querySelector('.dragDownLabel').innerHTML = parameter.dragDownLabel;
		document.addEventListener('touchmove', function(e) {
			e.preventDefault();
		}, false);
	};
	
	
$.fn.dragrefresh = function(options, params) {
		
		
		if(typeof options == "object" ){
			
			return this.each(function(){
				
				$this = $(this);
				var opts = $.fn.extend({}, $.fn.dragrefresh.defaults, options);
				$this.data('data',opts);
				var dragDown = '<div class="dragDown">'
									+'<div class="loader">'
										+'<span></span>'
										+'<span></span>'
										+'<span></span>'
										+'<span></span>'
									+'</div>'
									+'<div class="dragDownLabel">'
										+opts.dragDownLabel
									+'</div>'
								+'</div>'
								;
				var $dragDown = $(dragDown)
				$this.prepend($dragDown);
				
				var $content = '<div class="dragcontent"></div>';
				$this.append($content);
				
				
				var dragUp = '<div class="dragUp">'
									+'<div class="loader">'
									  +'<span></span>'
									  +'<span></span>'
									  +'<span></span>'
									  +'<span></span>'
								+'</div>'
								+'<div class="dragUpLabel">'+opts.dragUpLabel+'</div>'
							+'</div>'
							;
				var $dragUp = $(dragUp)
				$this.append($dragUp);
				
				
				var pullDownEl = $this.get(0).querySelector(".dragDown");
				var pullDownOffset = pullDownEl.offsetHeight;
				var pullUpEl = $this.get(0).querySelector(".dragUp");
				var pullUpOffset = pullUpEl.offsetHeight;
				scrollIt(opts, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset);
				opts.init($content);//调用初始化方法
				
			});
		}  else if(typeof options == "string"){
			return this.each(function(){
				$this = $(this);
				var opts = $this.data('picker');
				
				
				switch(options){
				//刷新参数
				case 'refreshOpts':
					if(typeof params == 'object'){
						opts = $.extend(opts,params);
						$this.data('picker',opts);
					}
					break;
				}
			});
			
		}
		
	};
	 $.fn.dragrefresh.defaults = {
			 id:'',
			 wrapper:null,
			 item:null,
			 dragDownLabel:'下拉刷新...',
			 draggingDownLabel:'释放刷新...',
			 draggingUpLabel:'释放加载更多...',
			 dragUpLabel:'上拉加载更多...',
			 init:function(){},
			 onUp:function(){},
			 onDown:function(){}
	};
})(jQuery);

